package com.auxgroup.hr.front.site.co.process;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.auxgroup.hr.front.dao.domain.*;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfAsaHdrRespVO;
import com.auxgroup.hr.front.site.vo.process.WfAsaSubRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.repository.PsBusUnitTblHrRepository;
import com.auxgroup.hr.front.dao.repository.PsCJobCurrVwRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import com.auxgroup.hr.front.service.process.ProcessService;
import com.auxgroup.hr.front.service.process.WfAsaHdrService;
import com.auxgroup.hr.front.service.process.WfFileService;
import com.auxgroup.hr.front.site.co.BaseCO;

/**
 * 岗位薪酬变动审批
 * @author lv-PC
 *
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfAsaHdr")
public class WfAsaHdrCO extends BaseWfCO {

	private static final Logger logger = LoggerFactory.getLogger(WfAsaHdrCO.class);

	@Autowired
	private WfAsaHdrService wfAsaHdrService;
	
	@Autowired
	private PsBusUnitTblHrRepository psBusUnitTblHrRepository;
	
	@Autowired
	private PsCJobCurrVwRepository PsCJobCurrVwRepository;
	
	@Autowired
	private PersonalDataService personalDataService;
	

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;
	
	/**
	 * 获取岗位薪酬变动审批信息
	 * @return
	 */
	@GetMapping(value = "/getAsaInfo")
	public BaseResponseVO getOne(String cTransactionNbr,@SessionGain("user") UserDTO user) {
		logger.info("getWfDrftHdr param= {}", cTransactionNbr);
		if(StringUtils.isBlank(cTransactionNbr)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
		}
		PsCWfAsaHdr asaHdr = wfAsaHdrService.getWfAsaHdr(cTransactionNbr);
		if(asaHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}
		WfAsaHdrRespVO respVo = objectMapper.map(asaHdr, WfAsaHdrRespVO.class);
		//设置业务系统取值
		if(StringUtils.isNotBlank(asaHdr.getcBusinessUnit())){
			PsBusUnitTblHr busUnit = psBusUnitTblHrRepository.findOne(asaHdr.getcBusinessUnit());
			if(busUnit!=null){
				respVo.setcBusinessUnitValue(busUnit.getDescr());
			}
		}
		
		//日常晋职晋薪明细数据
		List<PsCWfAsaSub1> subs = wfAsaHdrService.getWfAsaSubList(cTransactionNbr);
		buildSubsData(subs,respVo);
		fillBaseWfInfo(user, asaHdr, respVo);
		return buildRespone(respVo);
	}
	
	/**
	 * 构建日常晋职晋薪明细数据
	 */
	private void buildSubsData(List<PsCWfAsaSub1> subs,WfAsaHdrRespVO respVo){
		List<WfAsaSubRespVO> subsData = new ArrayList<WfAsaSubRespVO>();
		if(subs!=null&&!subs.isEmpty()){
			for (PsCWfAsaSub1 sub : subs) {
				WfAsaSubRespVO subRespVO = objectMapper.map(sub, WfAsaSubRespVO.class);
				PsCPersonalData person = personalDataService.getPersonalData(sub.getEmplid());
				if(person!=null){
					subRespVO.setEmplName(person.getName());
				}
				JobCurrPrimary primary = new JobCurrPrimary();
				primary.setEmplid(sub.getEmplid());
				primary.setEmplRcd(new BigDecimal(0));
				PsCJobCurrVw jobCurr = PsCJobCurrVwRepository.findOne(primary);
				if(jobCurr!=null){
					subRespVO.setRsJobLevelBefore(jobCurr.getRsJobLevel());
				}
				subsData.add(subRespVO);
			}
			respVo.setAsaSubs(subsData);
		}
	}


	@Override
	protected Set<String> getAuthFileds() {
		return null;
	}
}
